package com.ssy.lingxi.product.serviceImpl.saleorder.invoicestype;

import com.ssy.lingxi.after.sale.api.feign.ReplaceGoodsControllerFeign;
import com.ssy.lingxi.after.sale.api.model.vo.request.ReplaceAddDeliveryGoodsVO;
import com.ssy.lingxi.after.sale.api.model.vo.request.ReplaceAddGoodsDetailVO;
import com.ssy.lingxi.after.sale.api.model.vo.request.VerifyDeliveryVO;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.product.api.model.request.warehouse.InvoicesAddRequest;
import com.ssy.lingxi.product.api.model.request.warehouse.InvoicesUpdataRequest;
import com.ssy.lingxi.product.entity.warehouse.Invoices;
import com.ssy.lingxi.product.service.saleorder.invoicestype.InvoicesTypeStrategy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 售后换货退货发货单
 * @author wrc
 * @date 2020/12/21
 */
@Service("addReturnShipment")
public class AddReturnShipmentImpl implements InvoicesTypeStrategy {

    @Resource
    ReplaceGoodsControllerFeign replaceGoodsControllerFeign;

    /**
     * 添加单据-换货退货发货单
     */
    @Override
    public Wrapper invoicesAdd(Invoices invoices, InvoicesAddRequest request, UserLoginCacheDTO sysUser) {
        ReplaceAddDeliveryGoodsVO replaceAddDeliveryGoodsVO = new ReplaceAddDeliveryGoodsVO();
        replaceAddDeliveryGoodsVO.setReplaceId(request.getRelevanceInvoicesId());
        replaceAddDeliveryGoodsVO.setDeliveryNo(invoices.getInvoicesNo());
        replaceAddDeliveryGoodsVO.setDeliveryTime(request.getTransactionTime());
        replaceAddDeliveryGoodsVO.setDeliveryId(invoices.getId());
        List<ReplaceAddGoodsDetailVO> collect = request.getInvoicesDetailsRequests().stream().map(temp -> {
            ReplaceAddGoodsDetailVO replaceAddGoodsDetailVO = new ReplaceAddGoodsDetailVO();
            replaceAddGoodsDetailVO.setBrand(temp.getBrand());
            replaceAddGoodsDetailVO.setCategory(temp.getCategory());
            //TODO replaceAddGoodsDetailVO.setProductId(temp.getProductId());
            replaceAddGoodsDetailVO.setProductName(temp.getProductName());
            replaceAddGoodsDetailVO.setUnit(temp.getUnit());
            replaceAddGoodsDetailVO.setOrderNo(temp.getOrderNo());
            replaceAddGoodsDetailVO.setCount(temp.getShipmentQuantity());
            return replaceAddGoodsDetailVO;
        }).collect(Collectors.toList());
        replaceAddDeliveryGoodsVO.setDetailList(collect);
        replaceAddDeliveryGoodsVO.setMember(sysUser);
        return replaceGoodsControllerFeign.addReturnDeliveryGoods(replaceAddDeliveryGoodsVO);
    }

    /**
     * 修改单据业务处理基类
     */
    @Override
    public Wrapper invoicesUpdata(Invoices invoices, InvoicesUpdataRequest request) {
        return null;
    }

    /**
     * 批量审核单据
     * 售后换货退货发货单
     * @author wrc
     * @date 2020/12/29
     */
    @Override
    public Wrapper invoicesReviewAll(List<Invoices> invoicess, UserLoginCacheDTO sysUser) {
        for (Invoices invoices : invoicess) {
            VerifyDeliveryVO verifyDeliveryVO = new VerifyDeliveryVO();
            verifyDeliveryVO.setDataId(invoices.getId());
            verifyDeliveryVO.setMember(sysUser);
            Wrapper wrapper = replaceGoodsControllerFeign.verifyReturnDeliveryGoods(verifyDeliveryVO);
            if (ResponseCode.SUCCESS.getCode() != wrapper.getCode()) {
                return wrapper;
            }
        }
        return Wrapper.success();
    }
}
